package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Transactional
@Service(interfaceClass = OrderService.class)
public class OrderServiceImp implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Override
    public Result order(Map map) throws Exception {
        //检查用户所选择的预约日期是否已经进行了提前的预约设置,如果没有设置则无法进行预约
        String orderDate_Str = (String) map.get("orderDate");
        Date orderDate_Date= DateUtils.parseString2Date(orderDate_Str);
        Integer setmealId =Integer.parseInt((String)map.get("setmealId"));
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate_Date);
        if (orderSetting == null) {
            //指定日期没有进行预约设置,则无法预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //检查当日的已预约人数是否大于等于可预约人数,如果不是的话则无法进行预约
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (number <= reservations) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //检查用户是否是会员,如果是会员则直接完成预约,如果不是的话则自动完成注册并进行预约
        String telephone=(String)map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if(member!=null){
            //检查用户是否在同一天对同一个套餐进行了重复预约,如果是的话则无法进行再次预约
            Integer memberId = member.getId();

            Order order = new Order();
            order.setMemberId(memberId);
            order.setSetmealId(setmealId);
            order.setOrderDate(orderDate_Date);
            List<Order> orderList = orderDao.findByCondition(order);
            if(orderList!=null&&orderList.size()>0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else {
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setName((String)map.get("name"));
            member.setSex((String)map.get("sex"));
            member.setIdCard((String)map.get("idCard"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }

        //预约成功,更新当日的已预约人数
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate_Date);
        order.setOrderType((String)map.get("orderType"));
        order.setOrderStatus(order.ORDERSTATUS_NO);
        order.setSetmealId(setmealId);
        //往预约表中添加预约信息
        orderDao.add(order);
        //更新已预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.editNumberByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    //根据预约id查询相关信息(体检人姓名,预约日期,套餐名称,预约类型)
    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if(map!=null){
            Date orderDate =(Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
